package com.huawei.hianalytics.log.g;

import android.text.TextUtils;
import android.util.Log;
import com.huawei.hianalytics.a.d;
import com.huawei.hianalytics.g.b;
import java.io.BufferedWriter;
import java.io.Closeable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Locale;

public class f implements e {
    private com.huawei.hianalytics.log.c.a a;
    private String b;
    private String c;
    private Throwable d;

    static class a implements Serializable, Comparator<File> {
        private a() {
        }

        public int a(File file, File file2) {
            return (int) (file.length() - file2.length());
        }

        public /* synthetic */ int compare(Object obj, Object obj2) {
            return a((File) obj, (File) obj2);
        }
    }

    public f(com.huawei.hianalytics.log.c.a aVar, Throwable th, String str) {
        this.a = aVar;
        this.c = str + com.huawei.hianalytics.log.b.a.a.b;
        this.d = th;
    }

    private void a() {
        Closeable fileOutputStream;
        Closeable outputStreamWriter;
        Closeable closeable;
        Throwable th;
        Throwable th2;
        Closeable closeable2 = null;
        File file = new File(this.c, this.b);
        if (!com.huawei.hianalytics.log.f.a.a(file)) {
            String a = com.huawei.hianalytics.log.e.f.a();
            d.c(a);
            file = com.huawei.hianalytics.log.f.a.a(this.c, a, d.f());
        }
        if (file == null) {
            b.d("LogWrite", "writerLog():Failed to create file!");
            return;
        }
        try {
            fileOutputStream = new FileOutputStream(file, true);
            try {
                outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
            } catch (FileNotFoundException e) {
                closeable = null;
                outputStreamWriter = fileOutputStream;
                try {
                    b.d("LogWrite", "writeLog() No files that need to be written!");
                    com.huawei.hianalytics.log.e.d.a(9, closeable);
                    com.huawei.hianalytics.log.e.d.a(10, closeable2);
                    com.huawei.hianalytics.log.e.d.a(0, outputStreamWriter);
                } catch (Throwable th3) {
                    th = th3;
                    fileOutputStream = outputStreamWriter;
                    outputStreamWriter = closeable2;
                    closeable2 = closeable;
                    th2 = th;
                    com.huawei.hianalytics.log.e.d.a(9, closeable2);
                    com.huawei.hianalytics.log.e.d.a(10, outputStreamWriter);
                    com.huawei.hianalytics.log.e.d.a(0, fileOutputStream);
                    throw th2;
                }
            } catch (UnsupportedEncodingException e2) {
                closeable = null;
                outputStreamWriter = null;
                try {
                    b.d("LogWrite", "writeLog() OutputStreamWriter - Unsupported coding format");
                    com.huawei.hianalytics.log.e.d.a(9, closeable);
                    com.huawei.hianalytics.log.e.d.a(10, outputStreamWriter);
                    com.huawei.hianalytics.log.e.d.a(0, fileOutputStream);
                } catch (Throwable th4) {
                    th = th4;
                    closeable2 = closeable;
                    th2 = th;
                    com.huawei.hianalytics.log.e.d.a(9, closeable2);
                    com.huawei.hianalytics.log.e.d.a(10, outputStreamWriter);
                    com.huawei.hianalytics.log.e.d.a(0, fileOutputStream);
                    throw th2;
                }
            } catch (IOException e3) {
                closeable = null;
                outputStreamWriter = null;
                b.d("LogWrite", "writeLog append IO Exception !");
                com.huawei.hianalytics.log.e.d.a(9, closeable);
                com.huawei.hianalytics.log.e.d.a(10, outputStreamWriter);
                com.huawei.hianalytics.log.e.d.a(0, fileOutputStream);
            } catch (Throwable th5) {
                th2 = th5;
                outputStreamWriter = null;
                com.huawei.hianalytics.log.e.d.a(9, closeable2);
                com.huawei.hianalytics.log.e.d.a(10, outputStreamWriter);
                com.huawei.hianalytics.log.e.d.a(0, fileOutputStream);
                throw th2;
            }
            try {
                closeable = new BufferedWriter(outputStreamWriter);
            } catch (FileNotFoundException e4) {
                closeable = null;
                closeable2 = outputStreamWriter;
                outputStreamWriter = fileOutputStream;
                b.d("LogWrite", "writeLog() No files that need to be written!");
                com.huawei.hianalytics.log.e.d.a(9, closeable);
                com.huawei.hianalytics.log.e.d.a(10, closeable2);
                com.huawei.hianalytics.log.e.d.a(0, outputStreamWriter);
            } catch (UnsupportedEncodingException e5) {
                closeable = null;
                b.d("LogWrite", "writeLog() OutputStreamWriter - Unsupported coding format");
                com.huawei.hianalytics.log.e.d.a(9, closeable);
                com.huawei.hianalytics.log.e.d.a(10, outputStreamWriter);
                com.huawei.hianalytics.log.e.d.a(0, fileOutputStream);
            } catch (IOException e6) {
                closeable = null;
                b.d("LogWrite", "writeLog append IO Exception !");
                com.huawei.hianalytics.log.e.d.a(9, closeable);
                com.huawei.hianalytics.log.e.d.a(10, outputStreamWriter);
                com.huawei.hianalytics.log.e.d.a(0, fileOutputStream);
            } catch (Throwable th6) {
                th2 = th6;
                com.huawei.hianalytics.log.e.d.a(9, closeable2);
                com.huawei.hianalytics.log.e.d.a(10, outputStreamWriter);
                com.huawei.hianalytics.log.e.d.a(0, fileOutputStream);
                throw th2;
            }
            try {
                closeable.append(String.format(Locale.getDefault(), "%s: %s/%s: %s", new Object[]{com.huawei.hianalytics.log.e.f.b(), this.a.a(), this.a.b(), this.a.c() + "\n" + Log.getStackTraceString(this.d)}));
                closeable.flush();
                com.huawei.hianalytics.log.e.d.a(9, closeable);
                com.huawei.hianalytics.log.e.d.a(10, outputStreamWriter);
                com.huawei.hianalytics.log.e.d.a(0, fileOutputStream);
            } catch (FileNotFoundException e7) {
                closeable2 = outputStreamWriter;
                outputStreamWriter = fileOutputStream;
                b.d("LogWrite", "writeLog() No files that need to be written!");
                com.huawei.hianalytics.log.e.d.a(9, closeable);
                com.huawei.hianalytics.log.e.d.a(10, closeable2);
                com.huawei.hianalytics.log.e.d.a(0, outputStreamWriter);
            } catch (UnsupportedEncodingException e8) {
                b.d("LogWrite", "writeLog() OutputStreamWriter - Unsupported coding format");
                com.huawei.hianalytics.log.e.d.a(9, closeable);
                com.huawei.hianalytics.log.e.d.a(10, outputStreamWriter);
                com.huawei.hianalytics.log.e.d.a(0, fileOutputStream);
            } catch (IOException e9) {
                b.d("LogWrite", "writeLog append IO Exception !");
                com.huawei.hianalytics.log.e.d.a(9, closeable);
                com.huawei.hianalytics.log.e.d.a(10, outputStreamWriter);
                com.huawei.hianalytics.log.e.d.a(0, fileOutputStream);
            }
        } catch (FileNotFoundException e10) {
            closeable = null;
            outputStreamWriter = null;
            b.d("LogWrite", "writeLog() No files that need to be written!");
            com.huawei.hianalytics.log.e.d.a(9, closeable);
            com.huawei.hianalytics.log.e.d.a(10, closeable2);
            com.huawei.hianalytics.log.e.d.a(0, outputStreamWriter);
        } catch (UnsupportedEncodingException e11) {
            closeable = null;
            outputStreamWriter = null;
            fileOutputStream = null;
            b.d("LogWrite", "writeLog() OutputStreamWriter - Unsupported coding format");
            com.huawei.hianalytics.log.e.d.a(9, closeable);
            com.huawei.hianalytics.log.e.d.a(10, outputStreamWriter);
            com.huawei.hianalytics.log.e.d.a(0, fileOutputStream);
        } catch (IOException e12) {
            closeable = null;
            outputStreamWriter = null;
            fileOutputStream = null;
            b.d("LogWrite", "writeLog append IO Exception !");
            com.huawei.hianalytics.log.e.d.a(9, closeable);
            com.huawei.hianalytics.log.e.d.a(10, outputStreamWriter);
            com.huawei.hianalytics.log.e.d.a(0, fileOutputStream);
        } catch (Throwable th7) {
            th2 = th7;
            outputStreamWriter = null;
            fileOutputStream = null;
            com.huawei.hianalytics.log.e.d.a(9, closeable2);
            com.huawei.hianalytics.log.e.d.a(10, outputStreamWriter);
            com.huawei.hianalytics.log.e.d.a(0, fileOutputStream);
            throw th2;
        }
    }

    private void a(String str) {
        this.b = d.d();
        if (TextUtils.isEmpty(this.b)) {
            File[] listFiles = new File(str).listFiles();
            com.huawei.hianalytics.log.e.a.a(str);
            if (listFiles == null || listFiles.length == 0) {
                this.b = com.huawei.hianalytics.log.e.f.a();
            } else if (listFiles.length == 1) {
                this.b = listFiles[0].getName();
            } else {
                listFiles = com.huawei.hianalytics.log.f.a.a(listFiles);
                Arrays.sort(listFiles, new a());
                this.b = listFiles[0].getName();
            }
        }
    }

    public void run() {
        File file = new File(this.c);
        if (file.exists()) {
            a(this.c);
        } else if (file.mkdirs()) {
            this.b = com.huawei.hianalytics.log.e.f.a();
            d.c(this.b);
        } else {
            b.d("LogWrite", "create logsfile fail!");
            return;
        }
        synchronized (f.class) {
            a();
        }
    }
}
